﻿#include <QEventLoop>
#include <QTimer>
#include "CCamerCtrMange.h"
#include "CLogOper.h"
#include "CGlobal.h"
#include "Public.h"

CCamerCtrMange::CCamerCtrMange(CDevDataCam *pDevDataCam)
{
	m_pDevDataCam = pDevDataCam;
	m_pCamerSdkOper = new CCamerSdkOper(pDevDataCam);
	m_pMutex = new QMutex(QMutex::Recursive);
	m_bhread.store(false);
	m_bShouldStop = false;
	m_bFrist = true;
	m_nCount = 0;
}

CCamerCtrMange::~CCamerCtrMange()
{
	// 1. 先设置停止标志
	m_bShouldStop = true;

	// 2. 停止工作线程
	ThreadStop();

	// 3. 安全停止线程
	if (isRunning())
	{
		quit();
		if (!wait(3000)) 
		{ // 等待3秒超时
			terminate();
			wait(1000);
		}
	}

	// 4. 清理资源（此时线程已完全停止）
	if (m_pCamerSdkOper) 
	{
		delete m_pCamerSdkOper;
		m_pCamerSdkOper = nullptr;
	}

	if (m_pMutex != nullptr) 
	{
		delete m_pMutex;
		m_pMutex = nullptr;
	}
}

void CCamerCtrMange::ThreadStart()
{
	QMutexLocker locker(m_pMutex);
	if (!m_bhread.load())
	{
		if (m_bFrist)
		{
			start();
			m_bFrist = false;
		}
		m_bhread.store(true);
	}
}

void CCamerCtrMange::ThreadStop()
{
	QMutexLocker locker(m_pMutex);
	if (m_bhread.load())
	{
		m_bhread.store(false);
		// 清空命令队列
		m_vecCtrQueue.clear();
	}
}

bool CCamerCtrMange::CtrDevice(SCtrInfo &stCtrInfo)
{
	// 检查停止标志
	if (m_bShouldStop) 
	{
		return false;
	}
	if (stCtrInfo.m_bCtrSync == true)
	{
		return ctrDeviceMy(stCtrInfo);
	}
	else
	{
		// 如果正在停止，不接受新任务
		if (m_bShouldStop || !m_bhread.load())
		{
			return false;
		}
		//如果线程没启动 开启线程
		if (!m_bhread.load())
			return false;
		if (m_pMutex->tryLock(3000))
		{
			// 检查指针有效性
			if (!m_pCamerSdkOper) 
			{
				return false;
			}
			if (m_vecCtrQueue.size() > 5)
				m_vecCtrQueue.clear();
			m_vecCtrQueue.push_back(stCtrInfo);
			m_pMutex->unlock();
			return true;
		}
		else
		{
			return false;
		}
	}
}

bool CCamerCtrMange::CtrDeviceEx(SCtrInfo stCtrInfo)
{
	//设备本体
	return CtrDevice(stCtrInfo);
}

bool CCamerCtrMange::ctrDeviceMy(SCtrInfo &stCtrInfo)
{
	// 检查停止标志和指针有效性
	if (m_bShouldStop || !m_pCamerSdkOper || !m_pDevDataCam) {
		return false;
	}
	int nCmd = stCtrInfo.m_nCtrCmd;
	bool bResult = true;
	switch (nCmd)
	{
	case enCNoCmdCamer:
		bResult = true;
		break;
	case enCLogin:
		bResult = login(stCtrInfo);
		break;
	case enCRealPlay:
		bResult = realPlay(stCtrInfo);
		break;
	case enCStopRealPlay:
		bResult = stopRealPlay(stCtrInfo);
		break;
	case enCCapture:
		bResult = capture(stCtrInfo);
		break;
	case enCGetImage:
		bResult = getImage(stCtrInfo);
		break;
	default:
		bResult = true;
		break;
	}
	return bResult;
}

bool CCamerCtrMange::login(SCtrInfo& stCtrInfo)
{
	// 检查指针有效性
	if (!m_pCamerSdkOper || !m_pDevDataCam || m_bShouldStop)
	{
		return false;
	}
	bool bResult = true;
	SExecLog sDevExecLog;
	sDevExecLog.m_sDevName = m_pDevDataCam->m_sCamerName;
	sDevExecLog.m_sFuncName = A2T("摄像头");
	QString sReInfo = "";
	bResult = m_pCamerSdkOper->login(sReInfo);
	sDevExecLog.m_sDescriptionl = sReInfo;
	if (!bResult)
	{
		sDevExecLog.m_nAlarmLevel = enDevAlarm;
	}
	else
	{
		sDevExecLog.m_nAlarmLevel = enWorkInfo;
	}
	CLogOper::instance().addMsg(sDevExecLog);
	return bResult;
}

bool CCamerCtrMange::realPlay(SCtrInfo& stCtrInfo)
{
	// 检查指针有效性
	if (!m_pCamerSdkOper || !m_pDevDataCam || m_bShouldStop)
	{
		return false;
	}
	bool bResult = true;
	QString sReInfo = "";
	if (stCtrInfo.m_pLabel != nullptr)
	{
		bResult = m_pCamerSdkOper->realPlay(stCtrInfo.m_nSubCtrDevType, stCtrInfo.m_hWid, stCtrInfo.m_pLabel, sReInfo);
	}
	else
	{
		bResult = false;
	}
	return bResult;
}

bool CCamerCtrMange::stopRealPlay(SCtrInfo& stCtrInfo)
{
	// 检查指针有效性
	if (!m_pCamerSdkOper || !m_pDevDataCam || m_bShouldStop)
	{
		return false;
	}
	bool bResult = true;
	QString sReInfo = "";
	if (stCtrInfo.m_hWid == NULL)
	{
		bResult = m_pCamerSdkOper->stopRealPlay(stCtrInfo.m_nSubCtrDevType, sReInfo);
	}
	else
	{
		bResult = m_pCamerSdkOper->stopRealPlay(stCtrInfo.m_hWid, sReInfo);
	}
	return bResult;
}

bool CCamerCtrMange::capture(SCtrInfo &stCtrInfo)
{
	// 检查指针有效性
	if (!m_pCamerSdkOper || !m_pDevDataCam || m_bShouldStop)
	{
		return false;
	}
	bool bResult = true;
	SExecLog sDevExecLog;
	sDevExecLog.m_sDevName = m_pDevDataCam->m_sCamerName;
	sDevExecLog.m_sFuncName = A2T("摄像头");
	QString sReInfo = "";
	bResult = m_pCamerSdkOper->capture(stCtrInfo.m_nSubCtrDevType, stCtrInfo.m_sImgUrl, sReInfo);
	sDevExecLog.m_sDescriptionl = sReInfo;
	stCtrInfo.m_sReData = sReInfo;
	if (!bResult)
	{
		sDevExecLog.m_nAlarmLevel = enDevAlarm;
	}
	else
	{
		sDevExecLog.m_nAlarmLevel = enWorkInfo;
	}
	CLogOper::instance().addMsg(sDevExecLog);
	return bResult;
}

bool CCamerCtrMange::getImage(SCtrInfo &stCtrInfo)
{
	// 检查指针有效性
	if (!m_pCamerSdkOper || !m_pDevDataCam || m_bShouldStop)
	{
		return false;
	}
	bool bResult = true;
	SExecLog sDevExecLog;
	QString sReInfo = "";
	bResult = m_pCamerSdkOper->getImage(stCtrInfo.m_sImgUrl, sReInfo);
	sDevExecLog.m_sDevName = m_pDevDataCam->m_sCamerName;
	sDevExecLog.m_sFuncName = A2T("摄像头");
	sDevExecLog.m_sDescriptionl = sReInfo;
	stCtrInfo.m_sReData = sReInfo;
	if (!bResult)
	{
		sDevExecLog.m_nAlarmLevel = enDevAlarm;
	}
	else
	{
		sDevExecLog.m_nAlarmLevel = enWorkInfo;
	}
	CLogOper::instance().addMsg(sDevExecLog);
	return bResult;
}

void CCamerCtrMange::run()
{
	while (!m_bShouldStop)
	{
		// 使用局部作用域管理锁，避免长时间持有锁
		{
			QMutexLocker locker(m_pMutex);

			// 检查停止标志和指针有效性
			if (!m_bhread.load() || m_bShouldStop || !m_pCamerSdkOper || !m_pDevDataCam)
			{
				sysSleep(10);
				continue;
			}

			// 定期设置校正数据
			if (m_nCount >= 5)
			{
				m_pCamerSdkOper->setCorrectData(
					m_pDevDataCam->m_dLeftRectifyValue,
					m_pDevDataCam->m_dRightRectifyValue,
					m_pDevDataCam->m_nRectifySetValue,
					m_pDevDataCam->m_nRectifySetValueTwo,
					m_pDevDataCam->m_BeltEdgePf1,
					m_pDevDataCam->m_BeltEdgePf2,
					m_pDevDataCam->m_BeltEdgePf3,
					m_pDevDataCam->m_BeltEdgePf4,
					m_pDevDataCam->m_nIsHaveBeltState,
					m_pDevDataCam->m_bBeltState
				);
				m_nCount = 0;
			}
			else
			{
				m_nCount++;
			}

			// 处理命令队列
			if (m_vecCtrQueue.size() > 0)
			{
				SCtrInfo stCtrInfo = m_vecCtrQueue[0];
				m_vecCtrQueue.remove(0);

				// 在长时间操作前释放锁
				locker.unlock();
				ctrDeviceMy(stCtrInfo);
				locker.relock();
			}
		}

		// 在锁外睡眠，避免阻塞其他线程
		if (!m_bShouldStop) {
			sysSleep(200);
		}
	}
}

